//
//  STVolumeValueObservationController.swift
//  iPhone
//
//  Created by Toj on 10/21/22.
//

import UIKit

class STVolumeValueObservationController: UIViewController {
    
    internal var volumeValueObservation: NSKeyValueObservation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        
        let button = UIButton()
        button.backgroundColor = .green
        view.addSubview(button)
        button.frame = CGRect(x: 0, y: 0, width: 300, height: 330)
        
        // print(#function)
        // someInit()
        // do {
        // let audioSession = AVAudioSession.sharedInstance()
        //
        // try audioSession.setActive(true) // <- Important
        //
        // volumeValueObservation = audioSession.observe(\.outputVolume) { av, _ in
        // print(">>> DEBUG: [ViewController:handleVolumeChange]", av.outputVolume)
        // }
        // } catch {}
    }
    
    
    func someInit() {
        // ios15废弃监听不到事件
        NotificationCenter.default.addObserver(self, selector: #selector(onVolumnChanged),
                                               name: Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
    }
    
    @objc func onVolumnChanged(noti: Notification) {
        print(noti)
    }
    
    @objc func systemVolumeDidChange() {
        print(#function)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
}
